<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .success {
            color: green
        }

        .error {
            color: red
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            用户名 <input v-model="username.value" />
            <span :class="username.class">{{username.tip}}</span>
        </div>
        <div>
            密码 <input v-model="userpwd.value" />
            <span :class="userpwd.class">
                {{userpwd.tip}}
            </span>
        </div>
        <div>
            手机号 <input v-model="usertel.value" />
            <span :class="usertel.class">
                {{usertel.tip}}
            </span>
        </div>
    </div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            username: {
                value: "",
                reg: /^[a-z_$][0-9a-z_$]{5,9}$/i,
                tip: "请输出数字字母_$组成并且不能以数字开头长度6到10位",
                class: "error"
            },
            userpwd: {
                value: "",
                reg: /^[a-z0-9]{6,10}$/i,
                tip: "密码数字字母组成长度6到10位",
                class: "error"

            },
            usertel: {
                value: "",
                reg: /^1[3-9][0-9]{9}$/,
                tip: "请输入常用的手机号",
                class: "error"
            }
        },
        methods: {
            checkAttr(attr, tip) {
                if (this[attr].reg.test(this[attr].value)) {
                    this[attr].tip = "√";
                    this[attr].class = "success";
                } else {
                    this[attr].tip = tip;
                    this[attr].class = "error";
                }
            }

        },
        watch: {
            username: {
                deep: true,
                handler() {
                    this.checkAttr("username", "请输出数字字母_$组成并且不能以数字开头长度6到10位")
                }

            },
            userpwd: {
                deep: true,
                handler() {
                    this.checkAttr("userpwd", "密码数字字母组成长度6到10位")
                }

            },
            usertel: {
                deep: true,
                handler() {
                    this.checkAttr("usertel", "请输入常用的手机号");
                }

            }
        }
    })


</script>

</html>